
var prefix = "/fin/payment"
$(function() {
	load();
	laydate({
		elem : '#payDateend'
	});
	laydate({
		elem : '#payDateStr'
	});

});

function load() {
	$('#exampleTable')
			.bootstrapTable(
					{
						method : 'get', // 服务器数据的请求方式 get or post
						url : prefix + "/list", // 服务器数据的加载地址
						iconSize : 'outline',
						toolbar : '#exampleToolbar',
						striped : true, // 设置为true会有隔行变色效果
						dataType : "json", // 服务器返回的数据类型
						pagination : true, // 设置为true会在底部显示分页条
						singleSelect : false, // 设置为true将禁止多选
						pageSize : 10, // 如果设置了分页，每页数据条数
						pageNumber : 1, // 如果设置了分布，首页页码
						showColumns : false, // 是否显示内容下拉框（选择显示的列）
						sidePagination : "server", // 设置在哪里进行分页，可选值为"client" 或者 "server"
						queryParams : function(params) {
							return {
								//说明：传入后台的参数包括offset开始索引，limit步长，sort排序列，order：desc或者,以及所有列的键值对
								limit: params.limit,
								offset:params.offset,
					            purGroupid:$('#searchName').val(),
								payDateend:$('#payDateend').val(),
								payDateStr:$('#payDateStr').val(),
								paymentName:$('#paymentName').val(),
								payee:$('#payee').val(),
								depetLeadername:$('#depetLeadername').val()
					           // username:$('#searchName').val()
							};
						},
						columns : [
								{
									checkbox : true
								},
								{
									visible : false,
									field : 'id', 
									title : '主键' 
								},
								{
									field : 'purGroupid',
									title : '采购合同编号'
								},
								{
									field : 'paymentProof',
									title : '付款凭证',
									formatter: function (value, row,index ) {
										return '<img  style="width: 100px;   height: 80px;    margin-top: -6px;cursor: pointer;" src='+row.paymentProof+' alt="无" >'
									}
								},
								{
									field : 'status', 
									title : '状态',
									formatter : function(value, row, index){
										if(value==1){
											return '<span class="label label-primary">新创建</span>';
										}else if(value==2){
											return '<span class="label label-primary">初审中</span>';
										}else if(value==3){
											return '<span class="label label-primary">终审中</span>';
										}else if(value==4){
											return '<span class="label label-primary">待付款</span>';
										}else if(value==5){
											return '<span class="label label-primary">已付款</span>';
										}else if(value==6){
											return '<span class="label label-primary">作废</span>';
										}
									}
								},
								{
									field : 'payDate', 
									title : '付款时间' 
								},
								{
									visible : false,
									field : 'purId', 
									title : '采购ID' 
								},
								{
									field : 'purAmount', 
									title : '采购合同总额' 
								},
								{
									field : 'appliAmount', 
									title : '申请金额' 
								},
								{
									field : 'payee', 
									title : '收款方' 
								},
								{
									field : 'payeeAccount', 
									title : '收款账号' 
								},
								{
									field : 'bankName', 
									title : '开户行' 
								},
								{
									visible : false,
									field : 'paymentComid', 
									title : '付款方' 
								},
								{
									field : 'paymentName', 
									title : '付款方' 
								},
								{
									field : 'paymentType', 
									title : '付款方式' ,
									formatter : function(value, row, index){
										if(value==1){
											return '银行转账';
										}else if(value==2){
											return '银行承兑';
										}else if(value==3){
											return '现金';
										}
									}
								},
								{
									field : 'summary', 
									title : '摘要' 
								},
								{
									field : 'remark', 
									title : '备注' 
								},
																{
									title : '操作',
									field : 'operation',
									align : 'center',
									formatter : function(value, row, index) {
										var f = '<a class="btn btn-success btn-sm" href="#" title="确认提交"  mce_href="#" onclick="confirmOk(\''
											+ row.id
											+ '\')"><i class="fa fa-check"></i></a> ';

										var e = '<a class="btn btn-primary btn-sm" href="#" mce_href="#" title="编辑" onclick="edit(\''
											+ row.id
											+ '\')"><i class="fa fa-edit"></i></a> ';
										var d = '<a class="btn btn-warning btn-sm" href="#" title="作废"  mce_href="#" onclick="remove(\''
											+ row.id
											+ '\')"><i class="fa fa-remove"></i></a> ';
										var first = '<a class="btn btn-success btn-sm '+s_firstconfir_h+'" href="#" title="审核"  mce_href="#" onclick="review(\''
											+ row.id
											+ '\')"><i class="fa fa-hand-paper-o"></i></a> ';
										var end = '<a class="btn btn-success btn-sm '+s_endconfirm_h+'" href="#" title="审核"  mce_href="#" onclick="endreview(\''
											+ row.id
											+ '\')"><i class="fa fa-hand-paper-o"></i></a> ';
										var payment = '<a class="btn btn-success btn-sm '+s_payment_h+'" href="#" title="付款"  mce_href="#" onclick="payment(\''
											+ row.id
											+ '\')"><i class="fa fa-paypal"></i></a> ';
										if(row.status == 1){
											return f +e + d ;
										}else if(row.status == 2){
											return first;
										}else if(row.status == 3){
											return end;
										}else if(row.status == 4){
											return payment ;
										}
									}
								} ]
					});
}
function reLoad() {
	$('#exampleTable').bootstrapTable('refresh');
}
function add() {
	layer.open({
		type : 2,
		title : '增加',
		maxmin : true,
		shadeClose : false, // 点击遮罩关闭层
		area : [ '800px', '520px' ],
		content : prefix + '/add' // iframe的url
	});
}
function edit(id) {
	layer.open({
		type : 2,
		title : '编辑',
		maxmin : true,
		shadeClose : false, // 点击遮罩关闭层
		area : [ '800px', '520px' ],
		content : prefix + '/edit/' + id // iframe的url
	});
}
//提交审核
function confirmOk(id) {
	layer.confirm('确定要提交审核吗？', {
		btn : [ '确定', '取消' ]
	}, function() {
		$.ajax({
			url : prefix+"/confirmfirst",
			type : "post",
			data : {
				'id' : id
			},
			success : function(r) {
				if (r.code==0) {
					layer.msg(r.msg);
					reLoad();
				}else{
					layer.msg(r.msg);
				}
			}
		});
	})
}
//作废
function remove(id) {
	layer.confirm('确定要删除选中的记录？', {
		btn : [ '确定', '取消' ]
	}, function() {
		$.ajax({
			url : prefix+"/remove",
			type : "post",
			data : {
				'id' : id
			},
			success : function(r) {
				if (r.code==0) {
					layer.msg(r.msg);
					reLoad();
				}else{
					layer.msg(r.msg);
				}
			}
		});
	})
}

//初审
function review(id) {

	layer.confirm('确定要审核通过该付款？', {
			btn : [ '确定', '取消' ]
		}, function() {
			$.ajax({
				url : prefix+"/reviewyes",
				type : "post",
				data : {
					'id' : id
				},
				success : function(r) {
					if (r.code==0) {
						layer.msg(r.msg);
						reLoad();
					}else{
						layer.msg(r.msg);
					}
				}
			});
		},
		function() {
			$.ajax({
				url : prefix+"/reviewno",
				type : "post",
				data : {
					'id' : id
				},
				success : function(r) {
					if (r.code==0) {
						layer.msg(r.msg);
						reLoad();
					}else{
						layer.msg(r.msg);
					}
				}
			});
		})

}

//终审
function endreview(id) {

	layer.confirm('确定要审核通过该付款？', {
			btn : [ '确定', '取消' ]
		}, function() {
			$.ajax({
				url : prefix+"/seccondyes",
				type : "post",
				data : {
					'id' : id
				},
				success : function(r) {
					if (r.code==0) {
						layer.msg(r.msg);
						reLoad();
					}else{
						layer.msg(r.msg);
					}
				}
			});
		},
		function() {
			$.ajax({
				url : prefix+"/seccondno",
				type : "post",
				data : {
					'id' : id
				},
				success : function(r) {
					if (r.code==0) {
						layer.msg(r.msg);
						reLoad();
					}else{
						layer.msg(r.msg);
					}
				}
			});
		})

}

//付款
function payment(id) {
	layer.confirm('确定要付款？', {
		btn : [ '确定', '取消' ]
	}, function() {
		$.ajax({
			url : prefix+"/payment",
			type : "post",
			data : {
				'id' : id
			},
			success : function(r) {
				if (r.code==0) {
					layer.msg(r.msg);
					reLoad();
				}else{
					layer.msg(r.msg);
				}
			}
		});
	})
}


function exportData() {
	var url =prefix + '/exportData';
	var depetLeadername=$('#depetLeadername').val();
	var	payee=$('#payee').val();
	var	paymentName=$('#paymentName').val();
	var	payDateStr=$('#payDateStr').val();
	var	payDateend=$('#payDateend').val();
	var	purGroupid=$('#searchName').val();
	if(depetLeadername != null){
		if (url.indexOf('?') > -1) {
			url += '&depetLeadername=' + depetLeadername;
		} else {
			url += '?depetLeadername=' + depetLeadername;
		}
	}
	if(payee != null){
		if (url.indexOf('?') > -1) {
			url += '&payee=' + payee;
		} else {
			url += '?payee=' + payee;
		}
	}
	if(paymentName != null){
		if (url.indexOf('?') > -1) {
			url += '&paymentName=' + paymentName;
		} else {
			url += '?paymentName=' + paymentName;
		}
	}
	if(payDateStr != null){
		if (url.indexOf('?') > -1) {
			url += '&payDateStr=' + payDateStr;
		} else {
			url += '?payDateStr=' + payDateStr;
		}
	}
	if(payDateend != null){
		if (url.indexOf('?') > -1) {
			url += '&payDateend=' + payDateend;
		} else {
			url += '?payDateend=' + payDateend;
		}
	}
	if(purGroupid != null){
		if (url.indexOf('?') > -1) {
			url += '&purGroupid=' + purGroupid;
		} else {
			url += '?purGroupid=' + purGroupid;
		}
	}
	var form = document.createElement('form');
	form.setAttribute('action', url);
	form.setAttribute('method', 'post');
	form.setAttribute('style', 'display:none');
	document.body.appendChild(form);
	form.submit();
	document.body.removeChild(form)
}